package com.ruoyi.workflow.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.workflow.domain.WorkflowMission;
import com.ruoyi.workflow.mapper.WorkflowMissionMapper;
import com.ruoyi.workflow.service.IWorkflowMissionService;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.engine.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 定期工作管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-13
 */
@Service
public class WorkflowMissionServiceImpl implements IWorkflowMissionService
{
    @Autowired
    private ProcessRuntime processRuntime;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private WorkflowMissionMapper workflowMissionMapper;

    /**
     * 查询定期工作管理
     * 
     * @param id 定期工作管理ID
     * @return 定期工作管理
     */
    @Override
    public WorkflowMission selectWorkflowMissionById(String id)
    {
        return workflowMissionMapper.selectWorkflowMissionById(id);
    }

    /**
     * 查询定期工作管理列表
     * 
     * @param workflowMission 定期工作管理
     * @return 定期工作管理
     */
    @Override
    public List<WorkflowMission> selectWorkflowMissionList(WorkflowMission workflowMission)
    {
        return workflowMissionMapper.selectWorkflowMissionList(workflowMission);
    }

    /**
     * 新增定期工作管理
     * 
     * @param workflowMission 定期工作管理
     * @return 结果
     */
    @Override
    public int insertWorkflowMission(WorkflowMission workflowMission)
    {
        String id = UUID.randomUUID().toString();
        workflowMission.setId(id);
        workflowMission.setCreateTime(DateUtils.getNowDate());
        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("TimeSet_Work")
                .withName(workflowMission.getTitle())
                .withBusinessKey(id)
                .build());
        workflowMission.setInstanceId(processInstance.getId());
        workflowMission.setState("0");
        workflowMission.setCreateName(SecurityUtils.getNickName());
        workflowMission.setCreateBy(SecurityUtils.getUsername());
        workflowMission.setCreateTime(DateUtils.getNowDate());
        return workflowMissionMapper.insertWorkflowMission(workflowMission);
    }

    /**
     * 修改定期工作管理
     * 
     * @param workflowMission 定期工作管理
     * @return 结果
     */
    @Override
    public int updateWorkflowMission(WorkflowMission workflowMission)
    {
        workflowMission.setUpdateTime(DateUtils.getNowDate());
        return workflowMissionMapper.updateWorkflowMission(workflowMission);
    }

    /**
     * 批量删除定期工作管理
     * 
     * @param ids 需要删除的定期工作管理ID
     * @return 结果
     */
    @Override
    public int deleteWorkflowMissionByIds(String[] ids)
    {
        return workflowMissionMapper.deleteWorkflowMissionByIds(ids);
    }

    /**
     * 删除定期工作管理信息
     * 
     * @param id 定期工作管理ID
     * @return 结果
     */
    @Override
    public int deleteWorkflowMissionById(String id)
    {
        return workflowMissionMapper.deleteWorkflowMissionById(id);
    }
}
